﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Linq;
using NHibernate.Tool.hbm2ddl;

using OpenAPI.Model.API;
using OpenAPI.Model.OAuth;
using OpenAPI.IDAL;
using DBUtility;

namespace OpenAPI.NhiDAL
{
    public class SchemaDAL : ISchema
    {
        public bool Create()
        {
            bool result_stat = false;

            try
            {
                SchemaExport export = new SchemaExport(NHibernateHelper._Configuration);
                export.Create(false, true);

                result_stat = true;
            }
            catch
            {
                throw;
            }

            return result_stat;
        }

        public bool Drop()
        {
            bool result_stat = false;

            try
            {
                SchemaExport export = new SchemaExport(NHibernateHelper._Configuration);
                export.Drop(false, true);
                result_stat = true;
            }
            catch
            {
            }

            return result_stat;
        }


        public void InitDB()
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            using (ITransaction trans = session.BeginTransaction())
            {
                try
                {
                    #region 方法分类

                    MethodCategoryInfo mc_1 = new MethodCategoryInfo();
                    mc_1.CATEGORY_CD = "1001";
                    mc_1.CATEGORY_NAME = "OAuth";
                    mc_1.CATEGORY_DESC = "OAuth类：请求令牌、验证令牌、换取访问令牌";
                    mc_1.CATEGORY_ORDER = "1001";
                    session.Merge(mc_1);

                    #endregion

                    #region 方法

                    #region OpenAPI.OAuth.RequestToken

                    MethodInfo m_1 = new MethodInfo();
                    m_1.METHOD_NAME = "OpenAPI.OAuth.RequestToken";
                    m_1.METHOD_TEXT = "请求令牌";
                    m_1.METHOD_DESC = "";
                    m_1.METHOD_RETURN = "";
                    m_1.METHOD_ORDER = "1001001";
                    m_1.USED_YN = "Y";
                    m_1.CATEGORY_CD = "1001";
                    session.Merge(m_1);

                    //UnSignArgs:oauth_consumer_key
                    MethodUnSignArgumentInfo m_1_unSignArg_1 = new MethodUnSignArgumentInfo();
                    MethodUnSignArgumentInfoPK m_1_unSignArg_1_pk = new MethodUnSignArgumentInfoPK();
                    m_1_unSignArg_1_pk.METHOD_NAME = m_1.METHOD_NAME;
                    m_1_unSignArg_1_pk.ARGUMENT_NAME = "oauth_consumer_key";
                    m_1_unSignArg_1.MethodUnSignArgumentPK = m_1_unSignArg_1_pk;
                    m_1_unSignArg_1.ARGUMENT_VALUE = "";
                    m_1_unSignArg_1.ARGUMENT_DATATYPE = "varchar(32)";
                    m_1_unSignArg_1.ARGUMENT_DESC = "消费方Key";
                    session.Merge(m_1_unSignArg_1);

                    //SignArgs:oauth_nonce
                    MethodSignArgumentInfo m_1_signArg_1 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_1_signArg_1_pk = new MethodSignArgumentInfoPK();
                    m_1_signArg_1_pk.METHOD_NAME = m_1.METHOD_NAME;
                    m_1_signArg_1_pk.ARGUMENT_NAME = "oauth_nonce";
                    m_1_signArg_1.MethodSignArgumentPK = m_1_signArg_1_pk;
                    m_1_signArg_1.ARGUMENT_VALUE = "";
                    m_1_signArg_1.ARGUMENT_DATATYPE = "varchar(50)";
                    m_1_signArg_1.ARGUMENT_DESC = "唯一值";
                    session.Merge(m_1_signArg_1);

                    //SignArgs:oauth_timestamp
                    MethodSignArgumentInfo m_1_signArg_2 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_1_signArg_2_pk = new MethodSignArgumentInfoPK();
                    m_1_signArg_2_pk.METHOD_NAME = m_1.METHOD_NAME;
                    m_1_signArg_2_pk.ARGUMENT_NAME = "oauth_timestamp";
                    m_1_signArg_2.MethodSignArgumentPK = m_1_signArg_2_pk;
                    m_1_signArg_2.ARGUMENT_VALUE = "";
                    m_1_signArg_2.ARGUMENT_DATATYPE = "bigint";
                    m_1_signArg_2.ARGUMENT_DESC = "时间戳";
                    session.Merge(m_1_signArg_2);

                    //SignArgs:oauth_timestamp
                    MethodSignArgumentInfo m_1_signArg_3 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_1_signArg_3_pk = new MethodSignArgumentInfoPK();
                    m_1_signArg_3_pk.METHOD_NAME = m_1.METHOD_NAME;
                    m_1_signArg_3_pk.ARGUMENT_NAME = "oauth_version";
                    m_1_signArg_3.MethodSignArgumentPK = m_1_signArg_3_pk;
                    m_1_signArg_3.ARGUMENT_VALUE = "1.0";
                    m_1_signArg_3.ARGUMENT_DATATYPE = "varchar(50)";
                    m_1_signArg_3.ARGUMENT_DESC = "版本";
                    session.Merge(m_1_signArg_3);

                    #endregion

                    #region OpenAPI.OAuth.RequestToken

                    MethodInfo m_2 = new MethodInfo();
                    m_2.METHOD_NAME = "OpenAPI.OAuth.AuthorizeToken";
                    m_2.METHOD_TEXT = "验证令牌";
                    m_2.METHOD_DESC = @"<html>  <head>   <title></title>  </head>  <body class=scayt-enabled>   <p>    验证Token是否合法</p>  </body> </html> ";
                    m_2.METHOD_RETURN = @"<html>  <head>   <title></title>  </head>  <body class=scayt-enabled>   <p>    true/false</p>  </body> </html> ";
                    m_2.METHOD_ORDER = "1001002";
                    m_2.USED_YN = "Y";
                    m_2.CATEGORY_CD = "1001";
                    session.Merge(m_2);

                    //UnSignArgs:oauth_consumer_key
                    MethodUnSignArgumentInfo m_2_unSignArg_1 = new MethodUnSignArgumentInfo();
                    MethodUnSignArgumentInfoPK m_2_unSignArg_1_pk = new MethodUnSignArgumentInfoPK();
                    m_2_unSignArg_1_pk.METHOD_NAME = m_2.METHOD_NAME;
                    m_2_unSignArg_1_pk.ARGUMENT_NAME = "oauth_consumer_key";
                    m_2_unSignArg_1.MethodUnSignArgumentPK = m_2_unSignArg_1_pk;
                    m_2_unSignArg_1.ARGUMENT_VALUE = "";
                    m_2_unSignArg_1.ARGUMENT_DATATYPE = "varchar(32)";
                    m_2_unSignArg_1.ARGUMENT_DESC = "消费方Key";
                    session.Merge(m_2_unSignArg_1);

                    //UnSignArgs:oauth_token
                    MethodUnSignArgumentInfo m_2_unSignArg_2 = new MethodUnSignArgumentInfo();
                    MethodUnSignArgumentInfoPK m_2_unSignArg_2_pk = new MethodUnSignArgumentInfoPK();
                    m_2_unSignArg_2_pk.METHOD_NAME = m_2.METHOD_NAME;
                    m_2_unSignArg_2_pk.ARGUMENT_NAME = "oauth_token";
                    m_2_unSignArg_2.MethodUnSignArgumentPK = m_2_unSignArg_2_pk;
                    m_2_unSignArg_2.ARGUMENT_VALUE = "";
                    m_2_unSignArg_2.ARGUMENT_DATATYPE = "varchar(32)";
                    m_2_unSignArg_2.ARGUMENT_DESC = "令牌";
                    session.Merge(m_2_unSignArg_2);


                    //SignArgs:oauth_nonce
                    MethodSignArgumentInfo m_2_signArg_1 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_2_signArg_1_pk = new MethodSignArgumentInfoPK();
                    m_2_signArg_1_pk.METHOD_NAME = m_2.METHOD_NAME;
                    m_2_signArg_1_pk.ARGUMENT_NAME = "oauth_nonce";
                    m_2_signArg_1.MethodSignArgumentPK = m_2_signArg_1_pk;
                    m_2_signArg_1.ARGUMENT_VALUE = "";
                    m_2_signArg_1.ARGUMENT_DATATYPE = "varchar(50)";
                    m_2_signArg_1.ARGUMENT_DESC = "唯一值";
                    session.Merge(m_2_signArg_1);

                    //SignArgs:oauth_timestamp
                    MethodSignArgumentInfo m_2_signArg_2 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_2_signArg_2_pk = new MethodSignArgumentInfoPK();
                    m_2_signArg_2_pk.METHOD_NAME = m_2.METHOD_NAME;
                    m_2_signArg_2_pk.ARGUMENT_NAME = "oauth_timestamp";
                    m_2_signArg_2.MethodSignArgumentPK = m_2_signArg_2_pk;
                    m_2_signArg_2.ARGUMENT_VALUE = "";
                    m_2_signArg_2.ARGUMENT_DATATYPE = "bigint";
                    m_2_signArg_2.ARGUMENT_DESC = "时间戳";
                    session.Merge(m_2_signArg_2);

                    //SignArgs:oauth_timestamp
                    MethodSignArgumentInfo m_2_signArg_3 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_2_signArg_3_pk = new MethodSignArgumentInfoPK();
                    m_2_signArg_3_pk.METHOD_NAME = m_2.METHOD_NAME;
                    m_2_signArg_3_pk.ARGUMENT_NAME = "oauth_version";
                    m_2_signArg_3.MethodSignArgumentPK = m_2_signArg_3_pk;
                    m_2_signArg_3.ARGUMENT_VALUE = "1.0";
                    m_2_signArg_3.ARGUMENT_DATATYPE = "varchar(50)";
                    m_2_signArg_3.ARGUMENT_DESC = "版本";
                    session.Merge(m_2_signArg_3);

                    #endregion
                    
                    #region OpenAPI.OAuth.ExchangeAccessToken

                    MethodInfo m_3 = new MethodInfo();
                    m_3.METHOD_NAME = "OpenAPI.OAuth.ExchangeAccessToken";
                    m_3.METHOD_TEXT = "换取访问令牌";
                    m_3.METHOD_DESC = @"<html>  <head>   <title></title>  </head>  <body class=scayt-enabled>   <p>    换取访问令牌</p>  </body> </html> ";
                    m_3.METHOD_RETURN = @"<html>  <head>   <title></title>  </head>  <body class=scayt-enabled>   <p>    -----</p>  </body> </html> ";
                    m_3.METHOD_ORDER = "1001003";
                    m_3.USED_YN = "Y";
                    m_3.CATEGORY_CD = "1001";
                    session.Merge(m_3);

                    //UnSignArgs:oauth_consumer_key
                    MethodUnSignArgumentInfo m_3_unSignArg_1 = new MethodUnSignArgumentInfo();
                    MethodUnSignArgumentInfoPK m_3_unSignArg_1_pk = new MethodUnSignArgumentInfoPK();
                    m_3_unSignArg_1_pk.METHOD_NAME = m_3.METHOD_NAME;
                    m_3_unSignArg_1_pk.ARGUMENT_NAME = "oauth_consumer_key";
                    m_3_unSignArg_1.MethodUnSignArgumentPK = m_3_unSignArg_1_pk;
                    m_3_unSignArg_1.ARGUMENT_VALUE = "";
                    m_3_unSignArg_1.ARGUMENT_DATATYPE = "varchar(32)";
                    m_3_unSignArg_1.ARGUMENT_DESC = "消费方Key";
                    session.Merge(m_3_unSignArg_1);

                    //UnSignArgs:oauth_token
                    MethodUnSignArgumentInfo m_3_unSignArg_2 = new MethodUnSignArgumentInfo();
                    MethodUnSignArgumentInfoPK m_3_unSignArg_2_pk = new MethodUnSignArgumentInfoPK();
                    m_3_unSignArg_2_pk.METHOD_NAME = m_3.METHOD_NAME;
                    m_3_unSignArg_2_pk.ARGUMENT_NAME = "oauth_token";
                    m_3_unSignArg_2.MethodUnSignArgumentPK = m_3_unSignArg_2_pk;
                    m_3_unSignArg_2.ARGUMENT_VALUE = "";
                    m_3_unSignArg_2.ARGUMENT_DATATYPE = "varchar(32)";
                    m_3_unSignArg_2.ARGUMENT_DESC = "令牌";
                    session.Merge(m_3_unSignArg_2);


                    //SignArgs:oauth_nonce
                    MethodSignArgumentInfo m_3_signArg_1 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_3_signArg_1_pk = new MethodSignArgumentInfoPK();
                    m_3_signArg_1_pk.METHOD_NAME = m_3.METHOD_NAME;
                    m_3_signArg_1_pk.ARGUMENT_NAME = "oauth_nonce";
                    m_3_signArg_1.MethodSignArgumentPK = m_3_signArg_1_pk;
                    m_3_signArg_1.ARGUMENT_VALUE = "";
                    m_3_signArg_1.ARGUMENT_DATATYPE = "varchar(50)";
                    m_3_signArg_1.ARGUMENT_DESC = "唯一值";
                    session.Merge(m_3_signArg_1);

                    //SignArgs:oauth_timestamp
                    MethodSignArgumentInfo m_3_signArg_2 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_3_signArg_2_pk = new MethodSignArgumentInfoPK();
                    m_3_signArg_2_pk.METHOD_NAME = m_3.METHOD_NAME;
                    m_3_signArg_2_pk.ARGUMENT_NAME = "oauth_timestamp";
                    m_3_signArg_2.MethodSignArgumentPK = m_3_signArg_2_pk;
                    m_3_signArg_2.ARGUMENT_VALUE = "";
                    m_3_signArg_2.ARGUMENT_DATATYPE = "bigint";
                    m_3_signArg_2.ARGUMENT_DESC = "时间戳";
                    session.Merge(m_3_signArg_2);

                    //SignArgs:oauth_timestamp
                    MethodSignArgumentInfo m_3_signArg_3 = new MethodSignArgumentInfo();
                    MethodSignArgumentInfoPK m_3_signArg_3_pk = new MethodSignArgumentInfoPK();
                    m_3_signArg_3_pk.METHOD_NAME = m_3.METHOD_NAME;
                    m_3_signArg_3_pk.ARGUMENT_NAME = "oauth_version";
                    m_3_signArg_3.MethodSignArgumentPK = m_3_signArg_3_pk;
                    m_3_signArg_3.ARGUMENT_VALUE = "1.0";
                    m_3_signArg_3.ARGUMENT_DATATYPE = "varchar(50)";
                    m_3_signArg_3.ARGUMENT_DESC = "版本";
                    session.Merge(m_3_signArg_3);
                    #endregion

                    #endregion

                    #region 消费方等级

                    ConsumerLevelInfo lvl_1 = new ConsumerLevelInfo();
                    lvl_1.CONSUMER_LEVEL = "1001";
                    lvl_1.CONSUMER_LEVEL_NAME = "基本验证";
                    lvl_1.CONSUMER_LEVEL_DESC = "";
                    session.Merge(lvl_1);

                    MethodInConsumerLevelInfo lvl_m_1 = new MethodInConsumerLevelInfo();
                    MethodInConsumerLevelInfoPK lvl_m_1_pk = new MethodInConsumerLevelInfoPK();
                    lvl_m_1_pk.METHOD_NAME = m_1.METHOD_NAME;
                    lvl_m_1_pk.CONSUMER_LEVEL = lvl_1.CONSUMER_LEVEL;
                    lvl_m_1.MethodInConsumerLevelPK = lvl_m_1_pk;
                    session.Merge(lvl_m_1);

                    MethodInConsumerLevelInfo lvl_m_2 = new MethodInConsumerLevelInfo();
                    MethodInConsumerLevelInfoPK lvl_m_2_pk = new MethodInConsumerLevelInfoPK();
                    lvl_m_2_pk.METHOD_NAME = m_2.METHOD_NAME;
                    lvl_m_2_pk.CONSUMER_LEVEL = lvl_1.CONSUMER_LEVEL;
                    lvl_m_2.MethodInConsumerLevelPK = lvl_m_2_pk;
                    session.Merge(lvl_m_2);

                    MethodInConsumerLevelInfo lvl_m_3 = new MethodInConsumerLevelInfo();
                    MethodInConsumerLevelInfoPK lvl_m_3_pk = new MethodInConsumerLevelInfoPK();
                    lvl_m_3_pk.METHOD_NAME = m_3.METHOD_NAME;
                    lvl_m_3_pk.CONSUMER_LEVEL = lvl_1.CONSUMER_LEVEL;
                    lvl_m_3.MethodInConsumerLevelPK = lvl_m_3_pk;
                    session.Merge(lvl_m_3);

                    #endregion

                    #region 文档
                    DocumentInfo doc_0001001 = new DocumentInfo();
                    doc_0001001.DOC_CD = "0001001";
                    doc_0001001.DOC_SUBJECT = "概要";
                    doc_0001001.DOC_DATETIME = System.DateTime.Now;
                    doc_0001001.DOC_AUTHOR = "Manager";
                    doc_0001001.DOC_TAGS = "";
                    doc_0001001.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    <strong>OpenAPI</strong> 是为第三方（消费方）推出的开放API平台。平台采用REST形式发送请求（详细参照&ldquo;REST请求格式&rdquo;章节），采用OAuth规范进行身份验证，调用 OpenAPI时应遵循以下步骤：</p>   <p>    <strong>1、注册消费方（Consumer）</strong></p>   <p>    要调用 OpenAPI，您必需先注册一个Consumer，来获取Consumer Key和Consumer Secret（详细参见&ldquo;如何注册&rdquo;章节）。已注册的Consumer直接进入第2步。</p>   <p>    <strong>2、请求令牌（Token）</strong></p>   <p>    调用<u>OpenAPI.OAuth.RequestToken</u>&nbsp;方法请求Token及Token_Secret，调用时密钥使用Consumer Secret</p>   <p>    <strong>3、验证令牌<strong>（Token）</strong></strong></p>   <p>    调用<u>OpenAPI.OAuth.AuthorizeToken</u> 方法验证Token及Token_Secret，调用时密钥使用Token Secret</p>   <p>    验证通过后的Token及Token_Secret有一定的会话周期，会话周期内调用API时可直接使用；会话周期结束后需要执行第2步请求新的Token及Token_Secret（会话周期规定请继续关注本文，测试阶段会话周期不做限制）</p>   <p>    <strong>4、调用API</strong></p>   <p>    &nbsp;根据Consumer Key、Token、Token_Secret调用相关API，调用时密钥使用Token Secret</p>   <p>    &nbsp;</p>   <ul>    <li>     <strong>Open</strong><strong><strong>API请求URL地址</strong></strong></li>   </ul>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 100%;'>    <tbody>     <tr>      <td style='text-align: center;' width='80'>       开发</td>      <td>       <a href='http://youserverwebsite/api/rest' target='_blank'>http://youserverwebsite/api/rest</a></td>     </tr>     <tr>      <td style='text-align: center;' width='80'>       正式</td>      <td>       &nbsp;</td>     </tr>    </tbody>   </table></body> </html>";
                    session.Merge(doc_0001001);

                    DocumentInfo doc_0001002 = new DocumentInfo();
                    doc_0001002.DOC_CD = "0001002";
                    doc_0001002.DOC_SUBJECT = "术语";
                    doc_0001002.DOC_DATETIME = System.DateTime.Now;
                    doc_0001002.DOC_AUTHOR = "Manager";
                    doc_0001002.DOC_TAGS = "";
                    doc_0001002.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <ul>    <li>     <strong>服务提供方 Service Provider:</strong></li>   </ul>  </body> </html> <body class='scayt-enabled'>  <p>   <dd>    一个允许通过OAuth访问的web应用程序。</dd>  </p>  <p>   &nbsp;</p>  <ul>   <li>    <strong>用户 User:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    在服务提供方处拥有帐号的个人。&nbsp;&nbsp;</dd>  </p>  <p>   &nbsp;</p>  <ul>   <li>    <strong>消费方 Consumer:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    一个代表用户以OAuth形式访问服务提供方的网站或应用程序。    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong>受保护资源 Protected Resource(s):</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    服务提供方所掌控的数据，能被通过用户认证的消费方访问。    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>消费方开发者</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong> Consumer Developer:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    实现消费方的个人或组织。    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>消费方键值</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong> Consumer Key:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    消费方用来向服务提供方标示身份的值，参数为：oauth_consumer_key。    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong>消费方密钥 Consumer Secret:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    消费方用于建立对消费方键值所有权的密钥，参数为：oauth_consumer_secret。&nbsp;    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong>请求令牌 Request Token:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    消费方用于从用户处获得授权并换取请求令牌的值，参数为：oauth_token。&nbsp;    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong>访问令牌 Access Token:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    消费方用于代表用户在没有密码的情况下访问受保护资源的值，参数为：oauth_token。    <p>     &nbsp;</p>   </dd>  </p>  <ul>   <li>    <strong>令牌密钥 Token Secret:</strong></li>  </ul> </body> <body class='scayt-enabled'>  <p>   <dd>    消费方用于建立对特定令牌所有权的密钥，参数为：oauth_token_secret。    <p>     &nbsp;</p>   </dd>  </p> </body>";
                    session.Merge(doc_0001002);

                    DocumentInfo doc_0001003 = new DocumentInfo();
                    doc_0001003.DOC_CD = "0001003";
                    doc_0001003.DOC_SUBJECT = "错误代码";
                    doc_0001003.DOC_DATETIME = System.DateTime.Now;
                    doc_0001003.DOC_AUTHOR = "Manager";
                    doc_0001003.DOC_TAGS = "";
                    doc_0001003.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    暂未定义...<span _fck_bookmark='1' style='display: none'>&nbsp;</span></p></body> </html>";
                    session.Merge(doc_0001003);

                    DocumentInfo doc_0001004 = new DocumentInfo();
                    doc_0001004.DOC_CD = "0001004";
                    doc_0001004.DOC_SUBJECT = "如何注册";
                    doc_0001004.DOC_DATETIME = System.DateTime.Now;
                    doc_0001004.DOC_AUTHOR = "Manager";
                    doc_0001004.DOC_TAGS = "";
                    doc_0001004.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <ul>    <li>     <strong>为什么要注册？</strong></li>   </ul>   <p>    访问 OpenAPI必须拥有Consumer Key和Consumer Secret。</p>   <ul>    <li>     <strong>如何注册？</strong></li>   </ul>   <p>    要获取Consumer Key及Consumer Secret，需要消费方（Consumer）向服务提供方申请注册，服务提供方审核通过后会向消费方分配Consumer Key和Consumer Secret</p>   <ul>    <li>     <strong>注册时需提供什么信息？</strong></li>   </ul>   <p>    消费方注册时需要向服务提供方提供以下信息：</p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255,255,255); width: 100%'>    <thead>     <tr>      <th scope='col'>       条目</th>      <th scope='col'>       说明</th>     </tr>    </thead>    <tbody>     <tr>      <td width='150'>       消费方名称</td>      <td>       第三方名称</td>     </tr>     <tr>      <td width='150'>       消费方电子邮箱</td>      <td>       API变化时方便通知</td>     </tr>     <tr>      <td width='150'>       消费方OAuth请求URL地址</td>      <td>       <p>        消费方进行OAuth验证，获取令牌及密钥的请求URL地址（不包含参数部分）</p>       <p>        如：<a href='http://youwebsite/request_token' target='_blank'>http://youclientwebsite/request_token</a></p>      </td>     </tr>     <tr>      <td width='150'>       消费方OAuth回调URL地址</td>      <td>       <p>        消费方OAuth验证通过后的回调URL地址（不包含参数部分）</p>       <p>        如：<a href='http://youwebsite/request_token_ready' target='_blank'>http://youclientwebsite/request_token_ready</a></p>      </td>     </tr>    </tbody>   </table></body> </html>";
                    session.Merge(doc_0001004);

                    DocumentInfo doc_0001005 = new DocumentInfo();
                    doc_0001005.DOC_CD = "0001005";
                    doc_0001005.DOC_SUBJECT = "如何签名";
                    doc_0001005.DOC_DATETIME = System.DateTime.Now;
                    doc_0001005.DOC_AUTHOR = "Manager";
                    doc_0001005.DOC_TAGS = "";
                    doc_0001005.DOC_BODY = @"<html xmlns:cke=''>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    <strong>对 OpenAPI 进行REST请求时有如下几种参数：</strong></p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td width='100'>       API方法</td>      <td>       api_method</td>     </tr>     <tr>      <td width='100'>       不需签名参数</td>      <td>       UnSignArguments</td>     </tr>     <tr>      <td width='100'>       需签名参数</td>      <td>       SignArguments</td>     </tr>     <tr>      <td width='100'>       签名</td>      <td>       api_signature</td>     </tr>    </tbody>   </table>   <p>    <span style='color: rgb(255, 0, 0);'>注意：各参数值需要进行url encode编码（尤其中文时）</span></p>   <p>    <strong>调用API时需要对第3种参数进行签名，签名步骤如下：</strong></p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 100%;'>    <thead>     <tr>      <th scope='col' width='50'>       No.</th>      <th scope='col'>       说明</th>     </tr>    </thead>    <tbody>     <tr>      <td style='text-align: center;' width='50'>       1</td>      <td>       所有需签参数名称必须小写</td>     </tr>     <tr>      <td style='text-align: center;' width='50'>       2</td>      <td>       所有参数值要进行url encode编码（尤其中文时）,把编码结果转小写</td>     </tr>     <tr>      <td style='text-align: center;' width='50'>       3</td>      <td>       签名参数按字母升序排序</td>     </tr>     <tr>      <td style='text-align: center;' width='50'>       4</td>      <td>       合成签名字符串，合成时把&ldquo;签名密钥&rdquo;放到最前面，然后把已排序的<strong><u>需签名参数</u></strong>清单按照&ldquo;参数名称+参数值&rdquo;的方式追加到字符串</td>     </tr>     <tr>      <td style='text-align: center;' width='50'>       5</td>      <td>       对合成的签名字符串进行 UTF8 编码</td>     </tr>     <tr>      <td style='text-align: center;' width='50'>       6</td>      <td>       用MD5对签名字符串进行加密生成32位签名</td>     </tr>    </tbody>   </table>   <p>    第4步合成签名串规则如下：</p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px; height: 22px;'>    <tbody>     <tr>      <td style='text-align: center;'>       签名密钥</td>      <td style='text-align: center;'>       参数名称</td>      <td style='text-align: center;'>       参数值</td>      <td style='text-align: center;'>       参数名称</td>      <td style='text-align: center;'>       参数值</td>      <td style='text-align: center;'>       ......</td>     </tr>    </tbody>   </table>   <div style='page-break-after: always;'>    &nbsp;</div>   <p>    <strong>关于签名密钥的特殊说明：</strong></p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <thead>     <tr>      <th scope='col'>       调用API</th>      <th scope='col'>       使用的签名密钥</th>     </tr>    </thead>    <tbody>     <tr>      <td>       OpenAPI.OAuth.RequestToken</td>      <td>       oauth_consumer_secret</td>     </tr>     <tr>      <td>       Others</td>      <td>       oauth_token_secret</td>     </tr>    </tbody>   </table>   <p>    &nbsp;</p>   <p>    <strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>UTF8编码及MD5加密的C</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong>#参考实现代码如下：</p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       using System.Security;<br />       using System.Security.Cryptography;<br />       using System.Text;</td>     </tr>    </tbody>   </table>   <p>    &nbsp;</p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       //MD5加密<br />       public static string GetMd5String(string str)<br />       {<br />       // First we need to convert the string into bytes， which<br />       // means using a text encoder.<br />       byte[] unicodeText = System.Text.Encoding.UTF8.GetBytes(str);<br />       <br />       // Now that we have a byte array we can ask the CSP to hash it<br />       MD5 md5 = new MD5CryptoServiceProvider();<br />       byte[] result = md5.ComputeHash(unicodeText);<br />       <br />       // Build the final string by converting each byte<br />       // into hex and appending it to a StringBuilder<br />       StringBuilder sb = new StringBuilder();<br />       for (int i = 0; i &lt; result.Length; i++)<br />       {<br />       sb.Append(result[i].ToString(&quot;x2&quot;));<br />       }<br />       <br />       // And return it<br />       return sb.ToString();<br />       }</td>     </tr>    </tbody>   </table></body> </html>";
                    session.Merge(doc_0001005);

                    DocumentInfo doc_0001006 = new DocumentInfo();
                    doc_0001006.DOC_CD = "0001006";
                    doc_0001006.DOC_SUBJECT = "请求REST格式";
                    doc_0001006.DOC_DATETIME = System.DateTime.Now;
                    doc_0001006.DOC_AUTHOR = "Manager";
                    doc_0001006.DOC_TAGS = "";
                    doc_0001006.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    <strong>请求REST格式由以下几部分组成：</strong></p>   <ul>    <li>     请求REST地址，如：</li>   </ul>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       http://youserverwebsite/api/rest</td>     </tr>    </tbody>   </table>   <ul>    <li>     调用API方法，如：</li>   </ul>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       ?api_method=OpenAPI.OAuth.RequestToken</td>     </tr>    </tbody>   </table>   <ul>    <li>     不需签名参数，如：</li>   </ul>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       &amp;oauth_consumer_key=*************</td>     </tr>    </tbody>   </table>   <ul>    <li>     需签名参数，如：</li>   </ul>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       &amp;oauth_timestamp=****&amp;oauth_nonce=****&amp;oauth_version=****</td>     </tr>    </tbody>   </table>   <ul>    <li>     签名，如：</li>   </ul>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       &amp;api_signature=****</td>     </tr>    </tbody>   </table>   <p>    &nbsp;</p>   <p>    <strong>完整请求REST串：</strong></p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <tbody>     <tr>      <td>       <p>        http://youserverwebsite/api/rest?</p>       <p>        api_method=OpenAPI.oauth.requesttoken</p>       <p>        &amp;oauth_consumer_key=****</p>       <p>        &amp;oauth_nonce=a65c5e5e903942a994bcb07250431e2b</p>       <p>        &amp;oauth_timestamp=1277216551</p>       <p>        &amp;oauth_version=1.0</p>       <p>        &amp;api_signature=6431e9d5b8508a2177920e8c3c624b1b</p>      </td>     </tr>    </tbody>   </table></body> </html>";
                    session.Merge(doc_0001006);

                    DocumentInfo doc_0001007 = new DocumentInfo();
                    doc_0001007.DOC_CD = "0001007";
                    doc_0001007.DOC_SUBJECT = "请求REST返回格式";
                    doc_0001007.DOC_DATETIME = System.DateTime.Now;
                    doc_0001007.DOC_AUTHOR = "Manager";
                    doc_0001007.DOC_TAGS = "";
                    doc_0001007.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    请求REST返回格式为xml格式或者json格式，目前仅支持xml格式，示例如下：</p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 99%;'>    <tbody>     <tr>      <td>       &lt;rsp method=&quot;openapi.oauth.authorizetoken&quot; flag=&quot;True&quot; code=&quot;&quot; desc=&quot;ok&quot;&gt;<br />       &nbsp; &lt;data&gt;&nbsp;&nbsp;&nbsp; &lt;token&gt;4941c6de38650e58e75a1536d847c2b6&lt;/token&gt;<br />       &nbsp;&nbsp;&nbsp; &lt;token_secret&gt;c48bcf5ba5166a5b5c409e233811025b&lt;/token_secret&gt;&nbsp; &lt;/data&gt;&lt;/rsp&gt;</td>     </tr>    </tbody>   </table>   <p>    返回结果格式说明：</p>   <table border='1' cellpadding='0' cellspacing='0' style='background-color: rgb(255, 255, 255); width: 500px;'>    <thead>     <tr>      <th scope='col'>       参数名称</th>      <th scope='col'>       备注说明</th>     </tr>    </thead>    <tbody>     <tr>      <td width='100'>       method</td>      <td>       请求的API方法名称</td>     </tr>     <tr>      <td width='100'>       flag</td>      <td>       请求成功与否标识：true=成功；false=失败；</td>     </tr>     <tr>      <td width='100'>       code</td>      <td>       结果状态码</td>     </tr>     <tr>      <td width='100'>       desc</td>      <td>       结果说明</td>     </tr>     <tr>      <td width='100'>       data</td>      <td>       结果数据</td>     </tr>    </tbody>   </table></body> </html>";
                    session.Merge(doc_0001007);


                    DocumentInfo doc_9001001 = new DocumentInfo();
                    doc_9001001.DOC_CD = "9001001";
                    doc_9001001.DOC_SUBJECT = "APIClientHelper [C#]";
                    doc_9001001.DOC_DATETIME = System.DateTime.Now;
                    doc_9001001.DOC_AUTHOR = "Manager";
                    doc_9001001.DOC_TAGS = "";
                    doc_9001001.DOC_BODY = @"<html xmlns:cke=''>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    using System;<br />    using System.Collections;<br />    using System.Collections.Generic;<br />    using System.Linq;<br />    using System.Web;<br />    using System.Text;<br />    using System.Security;<br />    using System.Security.Cryptography;<br />    <br />    namespace OpenAPI.Web.Mvc.ClientDemo.Helper<br />    {<br />    &nbsp;&nbsp;&nbsp; public class APIClientHelper<br />    &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>   <p>    private static string API_URL = System.Configuration.ConfigurationManager.AppSettings[&quot;API_URL&quot;].ToString();//&quot;<a href='http://youserverwebsite/api/rest'>http://youserverwebsite/api/rest</a>&quot;;</p>   <p>    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <summary><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// 拼参数URL<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// </summary><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <param name='method' /><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <param name='unArguments' />不需签名参数列表<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <param name='arguments' />签名参数列表<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <param name='secret' /><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <returns></returns><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static string GetUrl(string method, ArrayList unArguments, ArrayList arguments, string secret)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder url = new StringBuilder();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder unsign_arguments_url = new StringBuilder();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder sign_arguments_url = new StringBuilder();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder sign = new StringBuilder();</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 合成Url的前半部分<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.AppendFormat(&quot;{0}?api_method={1}&quot;, API_URL, method);</p>   <p>    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //合成不需签名参数<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; unArguments.Count; i++)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArgumentInfo argument = (ArgumentInfo)unArguments[i];</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 合成<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsign_arguments_url.Append(&quot;&amp;&quot;);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsign_arguments_url.Append(argument.Name);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsign_arguments_url.Append(&quot;=&quot;);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsign_arguments_url.Append(HttpUtility.UrlEncode(argument.Value)); //对参数值进行UrlEncode编码<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>   <p>    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //排序需签名参数<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arguments.Sort();</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //加入密钥<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign.Append(secret);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //合成需签名参数<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; arguments.Count; i++)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArgumentInfo argument = (ArgumentInfo)arguments[i];</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 合成<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign_arguments_url.Append(&quot;&amp;&quot;);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign_arguments_url.Append(argument.Name);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign_arguments_url.Append(&quot;=&quot;);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign_arguments_url.Append(HttpUtility.UrlEncode(argument.Value)); //对参数值进行UrlEncode编码</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //合成签名<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign.Append(argument.Name);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sign.Append(HttpUtility.UrlEncode(argument.Value).ToLower()); //对参数值进行UrlEncode编码<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 将字符串用MD5加密码，生成32位的签名<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string api_signautre = GetMd5String(sign.ToString());</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 合成整个URL<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Append(unsign_arguments_url);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Append(sign_arguments_url);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Append(&quot;&amp;api_signature=&quot;);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Append(api_signautre);</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return url.ToString();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>   <p>    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <summary><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// 获取新的Guid<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// </summary><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <returns></returns><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static string GetGuidString()<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Guid.NewGuid().ToString().Replace(&quot;-&quot;, &quot;&quot;).Replace(&quot;{&quot;, &quot;&quot;).Replace(&quot;}&quot;, &quot;&quot;).ToLower();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <summary><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// MD5加密<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// </summary><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <param name='str' /><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <returns></returns><br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static string GetMd5String(string str)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // First we need to convert the string into bytes, which<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // means using a text encoder.<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] unicodeText = System.Text.Encoding.UTF8.GetBytes(str);<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Now that we have a byte array we can ask the CSP to hash it<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MD5 md5 = new MD5CryptoServiceProvider();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] result = md5.ComputeHash(unicodeText);<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Build the final string by converting each byte<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // into hex and appending it to a StringBuilder<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder sb = new StringBuilder();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; result.Length; i++)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append(result[i].ToString(&quot;x2&quot;));<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // And return it<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sb.ToString();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />    <br />    &nbsp;&nbsp;&nbsp; }<br />    <br />    &nbsp;&nbsp;&nbsp; public class ArgumentInfo : IComparable<br />    &nbsp;&nbsp;&nbsp; {<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public ArgumentInfo(string name, string value)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Name = name.ToLower();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Value = value;<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string Name;<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string Value;<br />    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int CompareTo(object obj)<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this.Name.CompareTo(((ArgumentInfo)obj).Name);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />    <br />    &nbsp;&nbsp;&nbsp; }<br />    <br />    }</p>  </body> </html>";
                    session.Merge(doc_9001001);

                    DocumentInfo doc_9001002 = new DocumentInfo();
                    doc_9001002.DOC_CD = "9001002";
                    doc_9001002.DOC_SUBJECT = "合成REST请求URL [C#]";
                    doc_9001002.DOC_DATETIME = System.DateTime.Now;
                    doc_9001002.DOC_AUTHOR = "Manager";
                    doc_9001002.DOC_TAGS = "";
                    doc_9001002.DOC_BODY = @"<html>  <head>   <title></title>  </head>  <body class='scayt-enabled'>   <p>    using System;<br />    using System.Collections;<br />    using System.Collections.Generic;<br />    using System.Linq;<br />    using System.Xml;<br />    using System.Xml.Linq;<br />    using System.Web;<br />    using System.Web.UI.WebControls;<br />    using System.Web.Mvc;</p>   <p>    using OpenAPI.Web.Mvc.ClientDemo.Helper;</p>   <p>    namespace OpenAPI.Web.Mvc.ClientDemo.Controllers<br />    {<br />    &nbsp;&nbsp;&nbsp; public class ClientDemoController : Controller<br />    &nbsp;&nbsp;&nbsp; {</p>   <p>    public void CreateUrlDemo()<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string oauth_consumer_key = System.Configuration.ConfigurationManager.AppSettings[&quot;OAUTH_CONSUMER_KEY&quot;].ToString();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string oauth_consumer_secret = System.Configuration.ConfigurationManager.AppSettings[&quot;OAUTH_CONSUMER_SECRET&quot;].ToString();</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string url = &quot;&quot;;<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string method = &quot;OpenAPI.OAuth.RequestToken&quot;;<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string api_secret = oauth_consumer_secret;</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList unArguments = new ArrayList();<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList arguments = new ArrayList();</p>   <p>    <br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unArguments.Add(new ArgumentInfo(&quot;oauth_consumer_key&quot;, oauth_consumer_key));</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arguments.Add(new ArgumentInfo(&quot;oauth_timestamp&quot;, APIClientHelper.GetTimeStemp().ToString()));<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arguments.Add(new ArgumentInfo(&quot;oauth_nonce&quot;, APIClientHelper.GetGuidString()));<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arguments.Add(new ArgumentInfo(&quot;oauth_version&quot;, &quot;1.0&quot;));</p>   <p>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url = APIClientHelper.GetUrl(method, unArguments, arguments, api_secret);<br />    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>   <p>    &nbsp;&nbsp;&nbsp; }//<br />    }//</p>  </body> </html>";
                    session.Merge(doc_9001002);

                    #endregion

                    #region 注册一个消费方
                    ConsumerInfo consumer_1 = new ConsumerInfo();
                    consumer_1.OAUTH_CONSUMER_KEY = "1e4f4b095fcd545d36027904b642fbf0";
                    consumer_1.OAUTH_CONSUMER_SECRET = "32f2bfc882968a4bba727ecc4aa72210";
                    consumer_1.CONSUMER_NAME = "OpenAPI";
                    consumer_1.CONSUMER_REQUEST_URL = "r_url";
                    consumer_1.CONSUMER_CALLBACK_URL = "c_url";
                    consumer_1.CONSUMER_EMAIL = "";
                    consumer_1.CONSUMER_LEVEL = "1001";
                    consumer_1.CONSUMER_STATUS = 1;
                    session.Merge(consumer_1);

                    #endregion

                    session.Flush();
                    trans.Commit();
                }
                catch (HibernateException)
                {
                    trans.Rollback();
                    throw;
                }
            }
            NHibernateHelper.CloseSession();
        }


    }
}
